package com.whli.jee.system.service.impl;

import com.whli.jee.common.util.DateUtils;
import com.whli.jee.common.util.WebUtils;
import com.whli.jee.common.web.dao.IBaseDao;
import com.whli.jee.common.web.service.impl.BaseServiceImpl;
import com.whli.jee.system.constant.SequenceConstant;
import com.whli.jee.system.dao.ISysBarcodeDao;
import com.whli.jee.system.dao.ISysSequenceDetailDao;
import com.whli.jee.system.entity.SysBarcode;
import com.whli.jee.system.entity.SysSequenceDetail;
import com.whli.jee.system.service.ISysBarcodeService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <em>类或方法作用描述</em>
 *
 * @author whli
 * @version 2018/9/4 9:26
 */
@Service("sysBarcodeService")
public class SysBarcodeServiceImpl extends BaseServiceImpl<SysBarcode> implements ISysBarcodeService {

    @Autowired
    private ISysBarcodeDao sysBarcodeDao;
    @Autowired
    private ISysSequenceDetailDao sysSequenceDetailDao;

    @Override
    public IBaseDao<SysBarcode> getDao() {
        return sysBarcodeDao;
    }

    @Override
    public synchronized String generateBarcode(String no) {
        List<SysSequenceDetail> detailList = sysSequenceDetailDao.getByBarcodeNo(no);
        if (CollectionUtils.isNotEmpty(detailList)){
            return handleSequenceDetail(detailList);
        }
        return null;
    }

    @Override
    public SysBarcode validByNo(String no, String id) {
        return sysBarcodeDao.validByNo(no,id);
    }

    /**
     * 处理序列规则详细
     * @param detailList
     * @return
     */
    private String handleSequenceDetail(List<SysSequenceDetail> detailList){
        String today = DateUtils.dateToString(new Date(),DateUtils.DEFAULT);
        StringBuffer barcode = new StringBuffer();
        for (SysSequenceDetail detail : detailList){ //字符
            String type = detail.getType();
            if (type.equals(SequenceConstant.TEXT)){
                barcode.append(detail.getValue());
            }else if (type.equals(SequenceConstant.NUMBER)){ //数字
                String update = DateUtils.dateToString(detail.getUpdateDate() == null ? new Date() : detail.getUpdateDate(),DateUtils.DEFAULT);
                //判断最后的更新日期与今天是否相符，如不相等设定值为1,否则值+1
                if (StringUtils.isBlank(detail.getValue()) || !today.equals(update)){
                    detail.setValue("1");
                }else{
                    int value = Integer.parseInt(detail.getValue());
                    ++value;
                    detail.setValue(value+"");
                }
                //判断数字值是否满足数字位数，不满足则在前位补0
                StringBuffer number = new StringBuffer(detail.getValue());
                if (number.length() < detail.getLength()){
                    int detailLength = detail.getLength() - number.length();
                    for (int i=0; i < detailLength; i++){
                        number.insert(0,"0");
                    }
                }
                barcode.append(number.toString());
                //更新数值
                SysSequenceDetail temp = new SysSequenceDetail();
                temp.setId(detail.getId());
                temp.setValue(detail.getValue());
                temp.setUpdateDate(new Date());
                temp.setUpdateBy(WebUtils.getLoginName());
                sysSequenceDetailDao.update(temp);
            }else if (type.equals(SequenceConstant.DATE)){ //日期
                String date = DateUtils.dateToString(new Date(),detail.getFormat());
                date = date.replace("-","").replace("/","")
                        .replace(" ","").replace(":","");
                barcode.append(date);
            }
            if (StringUtils.isNotBlank(detail.getDelimit())){
                barcode.append(detail.getDelimit());
            }
        }
        return barcode.toString();
    }
}
